package com.geeke.test.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.geeke.common.data.PageRequest;
import com.geeke.common.data.Parameter;
import com.geeke.test.dao.ExpenseDao;
import com.geeke.test.dao.ExpenseDetailDao;
import com.geeke.test.entity.Expense;
import com.geeke.test.entity.ExpenseDetail;
import com.geeke.utils.StringUtils;
import com.geeke.wf.service.NoneBizInProcService;
import com.google.common.collect.Lists;

/**
 * 报销管理Service
 * @author
 * @version
 */

@Service("expenseService")
@Transactional(readOnly = true)
public class ExpenseService extends NoneBizInProcService<ExpenseDao, Expense> {

    @Autowired
    private ExpenseDetailDao expenseDetailDao;

    @Autowired
    private ExpenseDetailService expenseDetailService;

    @Override
    public Expense get(String id) {
        Expense expense = super.get(id);

        List<Parameter> params = null;
        PageRequest pageRequest;

        /*获取子表列表   报销明细*/
        params = Lists.newArrayList();

        params.add(new Parameter("expense_id", "=", expense.getId()));
        pageRequest = new PageRequest(params);
        expense.setExpenseDetailList(expenseDetailDao.listAll(pageRequest));

        return expense;
    }

    /**
     * 删除
     * @param expense
     */
    @Override
    @Transactional(readOnly = false)
    public int delete(Expense expense) {
        List<Parameter> params = null;
        PageRequest pageRequest;

        /* 处理子表     报销明细 */
        params = Lists.newArrayList();

        params.add(new Parameter("expense_id", "=", expense.getId()));
        pageRequest = new PageRequest(params);
        expense.setExpenseDetailList(expenseDetailDao.listAll(pageRequest));

        if (expense.getExpenseDetailList() != null && expense.getExpenseDetailList().size() > 0) {
            expenseDetailService.bulkDelete(expense.getExpenseDetailList());
        }

        int rows = super.delete(expense);
        return rows;
    }

    @Override
    protected int doInsert(Expense expense) {
        int rows = dao.insert(expense);
        if (rows > 0) {
            /* 保存子表数据     报销明细 */
            saveExpenseDetailList(expense);
        }
        return rows;
    }

    @Override
    protected int doUpdate(Expense expense) {
        int rows = dao.update(expense);
        if (rows > 0) {
            /* 保存子表数据     报销明细 */
            saveExpenseDetailList(expense);
        }
        return rows;
    }

    /* 保存子表数据     报销明细 */
    private void saveExpenseDetailList(Expense expense) {
        List<Parameter> params = Lists.newArrayList();

        params.add(new Parameter("expense_id", "=", expense.getId()));
        PageRequest pageRequest = new PageRequest(params);
        List<ExpenseDetail> list_ExpenseDetail = expenseDetailDao.listAll(pageRequest);

        List<ExpenseDetail> deletes = Lists.newArrayList(); // 删除列表
        List<ExpenseDetail> inserts = Lists.newArrayList(); // 添加列表
        List<ExpenseDetail> updates = Lists.newArrayList(); // 更新列表
        for (ExpenseDetail expenseDetailSaved : list_ExpenseDetail) {
            boolean found = false;
            for (ExpenseDetail expenseDetail : expense.getExpenseDetailList()) {
                if (expenseDetailSaved.getId().equals(expenseDetail.getId())) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                deletes.add(expenseDetailSaved);
            }
        }
        if (deletes.size() > 0) {
            expenseDetailService.bulkDelete(deletes);
        }
        for (ExpenseDetail expenseDetail : expense.getExpenseDetailList()) {
            if (StringUtils.isBlank(expenseDetail.getId())) {
                expenseDetail.setExpense(expense);

                inserts.add(expenseDetail);
            } else {
                updates.add(expenseDetail);
            }
        }
        if (updates.size() > 0) {
            expenseDetailService.bulkUpdate(updates);
        }
        if (inserts.size() > 0) {
            expenseDetailService.bulkInsert(inserts);
        }
    }

    /**
     * 获取表单对象的class
     * @return
     */
    @Override
    protected Class<Expense> getBizClass() {
        return Expense.class;
    }
}
